package com.franklin.java.api.mybatis.annotation;

import com.franklin.java.api.annotation.BaseAnnotation;
import com.franklin.java.utils.PsiUtils;
import com.intellij.psi.PsiAnnotation;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
 * @author Franklin
 * @since 2021/08/22 12:34
 */
public class MyBatisAnnotations extends BaseAnnotation {

    static {
        annotationMap.put(new Param());
        annotationMap.put(new Select());
    }

    public static MyBatisAnnotation parse(@NotNull PsiAnnotation[] psiAnnotations){
        for (PsiAnnotation psiAnnotation : psiAnnotations) {
            MyBatisAnnotation myBatisAnnotation = parse(psiAnnotation);
            if (Objects.nonNull(myBatisAnnotation)){
                return myBatisAnnotation;
            }
        }
        return null;
    }

    public static MyBatisAnnotation parse(@NotNull PsiAnnotation psiAnnotation){
        MyBatisAnnotation parse = BaseAnnotation.parse(psiAnnotation, MyBatisAnnotation.class);
        if (Objects.nonNull(parse)){
            String value = PsiUtils.getAnnotationAttributeValue(psiAnnotation, "value");
            return new MyBatisAnnotation(
                parse.getQualifiedName(),
                parse.getShortName(),
                value
            ){};
        }
        return null;
    }

    public MyBatisAnnotations(String qualifiedName, String shortName) {
        super(qualifiedName, shortName);
    }

    private static class Param extends MyBatisAnnotation{

        private Param() {
            super("org.apache.ibatis.annotations.Param", "Param", null);
        }
    }

    public static class Select extends MyBatisAnnotation{

        private Select() {
            super("org.apache.ibatis.annotations.Select", "Select", null);
        }
    }
}
